import type { App, DirectiveBinding } from 'vue'
import { hasPermission } from '@/utils/permission'

export default {
  install(app: App<Element>) {
    /**
     * @description 自定义指令v-permissions
     */
    app.directive('permissions', {
      mounted(el: any, binding: DirectiveBinding) {
        
        const { value } = binding
        if (value)
          if (!hasPermission(value))
            el.parentNode && el.parentNode.removeChild(el)
      },
    }),
      //按钮防抖指令
      app.directive('repeat', {
        mounted(el: any, binding: DirectiveBinding) {
          el.addEventListener('click', () => {
            if (!el.disabled) {
              el.disabled = true
              el.classList.add('is-disabled')
              setTimeout(() => {
                el.disabled = false
                el.classList.remove('is-disabled')
              }, binding.value || 500)
            }
          })
        }
      })

  },
}
